<?php


namespace app\admin\controller\api;


use think\middleware\SessionInit;

class Wow extends Base
{
    /*
     *
     * */
    public function index(){
        // access_token 是否存在
        if(!session('access_token')){
            dump('不存在');
            $this->accessToken();
        }
        dump(session('access_token'));
        //WoW 代币指数（时光徽章）------------------------------------------------------------------------------------------------------------------------------------------------
        $url = "https://gateway.battlenet.com.cn/data/wow/token/index?namespace=dynamic-classic-cn&locale=zh_CN&access_token=".session('access_token');
        $timeBadge = $this->curl_get($url);
        $timeBadge['last_updated_timestamp'] = substr($timeBadge['last_updated_timestamp'],0,10);

        //处理后的参数（时光徽章）
        $data['last_updated_timestamp'] = date('Y-m-d H:i:s',$timeBadge['last_updated_timestamp']);
        $data['price'] = substr($timeBadge['price'],0,4);
        dump($data);
    }

    /*
     * 获取授权
     * return access_token
     * */
    public function accessToken(){
        if(session('access_token')){
            return session('access_token');
        }
        $client = new \GuzzleHttp\Client();
        $params = [
            'form_params' => [
                'client_id' => '255e388103bc43029a6acfb035c4dced',
                'client_secret' => 'qRfLaP1T2nfqpsRSKngibsYy9puJz6Ul',
                'grant_type' => 'client_credentials'
            ]
        ];
        $response = $client->request('POST', 'https://www.battlenet.com.cn/oauth/token',$params);

        if($response->getStatusCode() == 200){
//            echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8'
            $data = \GuzzleHttp\json_decode($response->getBody(),true);
            if(isset($data['access_token'])){
                session('access_token',$data['access_token']);
                return $data['access_token'];
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /*
     * get请求
     * */
    public function curl_get($url){
        $header = array(
        'Accept: application/json',
        );
        $curl = curl_init();
            //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
            //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
            // 超时设置,以秒为单位
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
        // 超时设置，以毫秒为单位
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
        // 设置请求头
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $data = curl_exec($curl);

        // 显示错误信息
        if (curl_error($curl)) {
            return curl_error($curl);
        } else {
            curl_close($curl);
            // 打印返回的内容
            return \GuzzleHttp\json_decode($data,true);
        }
    }

}